layui.use(["form", "jquery", "tree"], function (form, $, tree) {
    //渲染
    $.get(`${rootPath}/resource`, {roleId}, rs => {

        var inst1 = tree.render({
            elem: '#resource'  //绑定元素
            , data: rs.data
            , showCheckbox: true
            , showLine: false
            , id: 'resources'
        });
    }, 'json')
    form.on("submit(mapping-form)", e => {
        let data = {
            id: e.field.id,
            resourceIds: []
        }
        let resources = tree.getChecked("resources")

        function parseId(res) {
            for (let resource of res) {
                data.resourceIds.push(resource.id)
                if (resource.children && resource.children.length > 0) {
                    parseId(resource.children)
                }
            }
        }

        parseId(resources);
        $.ajax({
            type: 'post',
            url: `${rootPath}/resource/${roleId}`,
            dataType: 'json',
            data,
            success(rs) {
                if (rs.code === 0) {
                    // 在父页面弹窗提示窗口
                    parent.layer.msg(`修改${modelName}成功`, {icon: 6})
                    //重载父页面表格中的数据
                    parent.layui.table.reload('LAY-TABLE', {}, true)
                    // 关闭当前弹出框
                    let index = parent.layer.getFrameIndex(window.name)
                    parent.layer.close(index)
                } else {
                    layer.msg(`修改${modelName}失败`, {icon: 5})
                }
            }, error(e) {
                layer.msg(`修改${modelName}失败`, {icon: 5})
            }

        })
        return false;
    })
})